part of '../widgets.dart';

/// [VanCell]属性常量
const kVanCellFontSize = VanCssKey._raw('--van-cell-font-size');
const kVanCellLineHeight = VanCssKey._raw('--van-cell-line-height');
const kVanCellVerticalPadding = VanCssKey._raw('--van-cell-vertical-padding');
const kVanCellHorizontalPadding = VanCssKey._raw('--van-cell-horizontal-padding');
const kVanCellTextColor = VanCssKey._raw('--van-cell-text-color');
const kVanCellBackground = VanCssKey._raw('--van-cell-background');
const kVanCellBorderColor = VanCssKey._raw('--van-cell-border-color');
const kVanCellActiveColor = VanCssKey._raw('--van-cell-active-color');
const kVanCellRequiredColor = VanCssKey._raw('--van-cell-required-color');
const kVanCellLabelColor = VanCssKey._raw('--van-cell-label-color');
const kVanCellLabelFontSize = VanCssKey._raw('--van-cell-label-font-size');
const kVanCellLabelLineHeight = VanCssKey._raw('--van-cell-label-line-height');
const kVanCellLabelMarginTop = VanCssKey._raw('--van-cell-label-margin-top');
const kVanCellValueColor = VanCssKey._raw('--van-cell-value-color');
const kVanCellValueFontSize = VanCssKey._raw('--van-cell-value-font-size');
const kVanCellIconSize = VanCssKey._raw('--van-cell-icon-size');
const kVanCellRightIconColor = VanCssKey._raw('--van-cell-right-icon-color');
const kVanCellLargeVerticalPadding = VanCssKey._raw('--van-cell-large-vertical-padding');
const kVanCellLargeTitleFontSize = VanCssKey._raw('--van-cell-large-title-font-size');
const kVanCellLargeLabelFontSize = VanCssKey._raw('--van-cell-large-label-font-size');
const kVanCellLargeValueFontSize = VanCssKey._raw('--van-cell-large-value-font-size');

/// [VanCell]主题类
@immutable
class VanCellTheme extends VanProvider<VanCellThemeData> with VanProvide {
  const VanCellTheme({super.key, required super.child, required super.provide});

  static VanCellThemeData of(BuildContext context) => VanProvider.of<VanCellTheme>(context)?.provide ?? VanTheme.of(context).cellTheme;
  static const _prefix = "--van-cell-";

  static bool support(VanCssKey vanCssKey) => vanCssKey.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanCellThemeData cellTheme = of(context);
    if (kVanCellFontSize == key) return cellTheme.fontSize.get(context);
    if (kVanCellLineHeight == key) return cellTheme.lineHeight.get(context);
    if (kVanCellVerticalPadding == key) return cellTheme.verticalPadding.get(context);
    if (kVanCellHorizontalPadding == key) return cellTheme.horizontalPadding.get(context);
    if (kVanCellTextColor == key) return cellTheme.textColor.get(context);
    if (kVanCellBackground == key) return cellTheme.background.get(context);
    if (kVanCellBorderColor == key) return cellTheme.borderColor.get(context);
    if (kVanCellActiveColor == key) return cellTheme.activeColor.get(context);
    if (kVanCellRequiredColor == key) return cellTheme.requiredColor.get(context);
    if (kVanCellLabelColor == key) return cellTheme.labelColor.get(context);
    if (kVanCellLabelFontSize == key) return cellTheme.labelFontSize.get(context);
    if (kVanCellLabelLineHeight == key) return cellTheme.labelLineHeight.get(context);
    if (kVanCellLabelMarginTop == key) return cellTheme.labelMarginTop.get(context);
    if (kVanCellValueColor == key) return cellTheme.valueColor.get(context);
    if (kVanCellValueFontSize == key) return cellTheme.valueFontSize.get(context);
    if (kVanCellIconSize == key) return cellTheme.iconSize.get(context);
    if (kVanCellRightIconColor == key) return cellTheme.rightIconColor.get(context);
    if (kVanCellLargeVerticalPadding == key) return cellTheme.largeVerticalPadding.get(context);
    if (kVanCellLargeTitleFontSize == key) return cellTheme.largeTitleFontSize.get(context);
    if (kVanCellLargeLabelFontSize == key) return cellTheme.largeLabelFontSize.get(context);
    if (kVanCellLargeValueFontSize == key) return cellTheme.largeValueFontSize.get(context);
    return null;
  }
}

/// [VanCell]主题数据类
@immutable
class VanCellThemeData with VanProvide {
  final VanCss<double> fontSize;
  final VanCss<double> lineHeight;
  final VanCss<double> verticalPadding;
  final VanCss<double> horizontalPadding;
  final VanCss<Color> textColor;
  final VanCss<VanBgColor> background;
  final VanCss<Color> borderColor;
  final VanCss<Color> activeColor;
  final VanCss<Color> requiredColor;
  final VanCss<Color> labelColor;
  final VanCss<double> labelFontSize;
  final VanCss<double> labelLineHeight;
  final VanCss<double> labelMarginTop;
  final VanCss<Color> valueColor;
  final VanCss<double> valueFontSize;
  final VanCss<double> iconSize;
  final VanCss<Color> rightIconColor;
  final VanCss<double> largeVerticalPadding;
  final VanCss<double> largeTitleFontSize;
  final VanCss<double> largeLabelFontSize;
  final VanCss<double> largeValueFontSize;

  /// 构造器
  const VanCellThemeData.raw({
    required this.fontSize,
    required this.lineHeight,
    required this.verticalPadding,
    required this.horizontalPadding,
    required this.textColor,
    required this.background,
    required this.borderColor,
    required this.activeColor,
    required this.requiredColor,
    required this.labelColor,
    required this.labelFontSize,
    required this.labelLineHeight,
    required this.labelMarginTop,
    required this.valueColor,
    required this.valueFontSize,
    required this.iconSize,
    required this.rightIconColor,
    required this.largeVerticalPadding,
    required this.largeTitleFontSize,
    required this.largeLabelFontSize,
    required this.largeValueFontSize,
  });

  /// 拷贝
  VanCellThemeData copyFrom({
    VanCss<double>? fontSize,
    VanCss<double>? lineHeight,
    VanCss<double>? verticalPadding,
    VanCss<double>? horizontalPadding,
    VanCss<Color>? textColor,
    VanCss<VanBgColor>? background,
    VanCss<Color>? borderColor,
    VanCss<Color>? activeColor,
    VanCss<Color>? requiredColor,
    VanCss<Color>? labelColor,
    VanCss<double>? labelFontSize,
    VanCss<double>? labelLineHeight,
    VanCss<double>? labelMarginTop,
    VanCss<Color>? valueColor,
    VanCss<double>? valueFontSize,
    VanCss<double>? iconSize,
    VanCss<Color>? rightIconColor,
    VanCss<double>? largeVerticalPadding,
    VanCss<double>? largeTitleFontSize,
    VanCss<double>? largeLabelFontSize,
    VanCss<double>? largeValueFontSize,
  }) {
    return VanCellThemeData.raw(
      fontSize: fontSize ?? this.fontSize,
      lineHeight: lineHeight ?? this.lineHeight,
      verticalPadding: verticalPadding ?? this.verticalPadding,
      horizontalPadding: horizontalPadding ?? this.horizontalPadding,
      textColor: textColor ?? this.textColor,
      background: background ?? this.background,
      borderColor: borderColor ?? this.borderColor,
      activeColor: activeColor ?? this.activeColor,
      requiredColor: requiredColor ?? this.requiredColor,
      labelColor: labelColor ?? this.labelColor,
      labelFontSize: labelFontSize ?? this.labelFontSize,
      labelLineHeight: labelLineHeight ?? this.labelLineHeight,
      labelMarginTop: labelMarginTop ?? this.labelMarginTop,
      valueColor: valueColor ?? this.valueColor,
      valueFontSize: valueFontSize ?? this.valueFontSize,
      iconSize: iconSize ?? this.iconSize,
      rightIconColor: rightIconColor ?? this.rightIconColor,
      largeVerticalPadding: largeVerticalPadding ?? this.largeVerticalPadding,
      largeTitleFontSize: largeTitleFontSize ?? this.largeTitleFontSize,
      largeLabelFontSize: largeLabelFontSize ?? this.largeLabelFontSize,
      largeValueFontSize: largeValueFontSize ?? this.largeValueFontSize,
    );
  }

  /// 反序列化
  static VanCellThemeData fromJson(Map<String, dynamic> json) {
    return VanCellThemeData.raw(
      fontSize: VanCss(json[kVanCellFontSize.key]),
      lineHeight: VanCss(json[kVanCellLineHeight.key]),
      verticalPadding: VanCss(json[kVanCellVerticalPadding.key]),
      horizontalPadding: VanCss(json[kVanCellHorizontalPadding.key]),
      textColor: VanCss(json[kVanCellTextColor.key]),
      background: VanCss(json[kVanCellBackground.key]),
      borderColor: VanCss(json[kVanCellBorderColor.key]),
      activeColor: VanCss(json[kVanCellActiveColor.key]),
      requiredColor: VanCss(json[kVanCellRequiredColor.key]),
      labelColor: VanCss(json[kVanCellLabelColor.key]),
      labelFontSize: VanCss(json[kVanCellLabelFontSize.key]),
      labelLineHeight: VanCss(json[kVanCellLabelLineHeight.key]),
      labelMarginTop: VanCss(json[kVanCellLabelMarginTop.key]),
      valueColor: VanCss(json[kVanCellValueColor.key]),
      valueFontSize: VanCss(json[kVanCellValueFontSize.key]),
      iconSize: VanCss(json[kVanCellIconSize.key]),
      rightIconColor: VanCss(json[kVanCellRightIconColor.key]),
      largeVerticalPadding: VanCss(json[kVanCellLargeVerticalPadding.key]),
      largeTitleFontSize: VanCss(json[kVanCellLargeTitleFontSize.key]),
      largeLabelFontSize: VanCss(json[kVanCellLargeLabelFontSize.key]),
      largeValueFontSize: VanCss(json[kVanCellLargeValueFontSize.key]),
    );
  }
}

/// [VanCell]样式
@immutable
class VanCellStyle with VanProvide {
  MainAxisAlignment get _titleMainAxisAlignment {
    if (titleAlignment == Alignment.center) return MainAxisAlignment.center;
    if (titleAlignment == Alignment.centerRight) return MainAxisAlignment.end;
    return MainAxisAlignment.start;
  }

  static of(BuildContext context) => VanProvider.of<VanCellStyle>(context);

  final double? fontSize;
  final double? lineHeight;
  final EdgeInsets? padding;
  final Color? textColor;
  final VanBgColor? background;
  final Color? borderColor;
  final Color? activeColor;
  final Color? requiredColor;
  final Color? labelColor;
  final TextAlign? labelAlign;
  final double? labelFontSize;
  final double? labelWidth;
  final double? labelLineHeight;
  final double? labelMarginTop;
  final Color? valueColor;
  final double? valueFontSize;
  final double? iconSize;
  final Color? rightIconColor;
  final double? titleFontSize;
  final Alignment? titleAlignment;

  bool get isNonNull {
    if (fontSize == null) return false;
    if (lineHeight == null) return false;
    if (padding == null) return false;
    if (textColor == null) return false;
    if (background == null) return false;
    if (borderColor == null) return false;
    if (activeColor == null) return false;
    if (requiredColor == null) return false;
    if (labelColor == null) return false;
    if (labelAlign == null) return false;
    if (labelWidth == null) return false;
    if (labelFontSize == null) return false;
    if (labelLineHeight == null) return false;
    if (labelMarginTop == null) return false;
    if (valueColor == null) return false;
    if (valueFontSize == null) return false;
    if (iconSize == null) return false;
    if (rightIconColor == null) return false;
    if (titleFontSize == null) return false;
    if (titleAlignment == null) return false;
    return true;
  }

  VanCellStyle({
    this.fontSize,
    this.lineHeight,
    this.padding,
    this.textColor,
    this.background,
    this.borderColor,
    this.activeColor,
    this.requiredColor,
    this.labelColor,
    this.labelAlign,
    this.labelWidth,
    this.labelFontSize,
    this.labelLineHeight,
    this.labelMarginTop,
    this.valueColor,
    this.valueFontSize,
    this.iconSize,
    this.rightIconColor,
    this.titleFontSize,
    this.titleAlignment,
  });

  VanCellStyle copyFrom({
    double? fontSize,
    double? lineHeight,
    EdgeInsets? padding,
    Color? textColor,
    VanBgColor? background,
    Color? borderColor,
    Color? activeColor,
    Color? requiredColor,
    Color? labelColor,
    TextAlign? labelAlign,
    double? labelWidth,
    double? labelFontSize,
    double? labelLineHeight,
    double? labelMarginTop,
    Color? valueColor,
    double? valueFontSize,
    double? iconSize,
    Color? rightIconColor,
    double? titleFontSize,
    Alignment? titleAlignment,
  }) {
    return VanCellStyle(
      fontSize: fontSize ?? this.fontSize,
      lineHeight: lineHeight ?? this.lineHeight,
      padding: padding ?? this.padding,
      textColor: textColor ?? this.textColor,
      background: background ?? this.background,
      borderColor: borderColor ?? this.borderColor,
      activeColor: activeColor ?? this.activeColor,
      requiredColor: requiredColor ?? this.requiredColor,
      labelColor: labelColor ?? this.labelColor,
      labelAlign: labelAlign ?? this.labelAlign,
      labelWidth: labelWidth ?? this.labelWidth,
      labelFontSize: labelFontSize ?? this.labelFontSize,
      labelLineHeight: labelLineHeight ?? this.labelLineHeight,
      labelMarginTop: labelMarginTop ?? this.labelMarginTop,
      valueColor: valueColor ?? this.valueColor,
      valueFontSize: valueFontSize ?? this.valueFontSize,
      iconSize: iconSize ?? this.iconSize,
      rightIconColor: rightIconColor ?? this.rightIconColor,
      titleFontSize: titleFontSize ?? this.titleFontSize,
      titleAlignment: titleAlignment ?? this.titleAlignment,
    );
  }
}
